import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { viteMockServe } from 'vite-plugin-mock'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'


// https://vite.dev/config/
export default defineConfig(({ command }) => {
  /// 获取各种环境下对应的变量
  let env = loadEnv(command, process.cwd())
  return {
    plugins: [
      vue(),

      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        // Specify symbolId format
        symbolId: 'icon-[dir]-[name]',
      }),

      viteMockServe({
        mockPath: 'mock',
        enable: true
      }),

      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),

      Components({
        resolvers: [ElementPlusResolver()],
      })

    ],
    css: {
      preprocessorOptions: {
        scss: {
          // javascriptEnabled: true,
          // charset: false,
          additionalData: `@import "@/styles/variable.scss";`
        },
      },
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      }
    },
    // 代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          // 获取数据服务器地址设置
          target: env.VITE_SERVE,
          // 是否需要代理跨域
          changeOrigin: true,
          // 是否重写路径 【默认地址后边有’/api‘,如果使用时候没有’/api‘,需要替换掉】
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    }
  }
});
